package com.wxx.design.lazysingleton;

/**
 * @author Jetwang
 * @version 1.0
 * @date 2022/4/11 19:28
 * LazyDoubleCheckSingleton 双重检查 兼顾了性能和安全 懒汉式
 */
public class LazyDoubleCheckSingleton {
    /**
     * 添加volatile关键字,禁止重排序,保证可见性
     */
    private volatile static LazyDoubleCheckSingleton lazyDoubleCheckSingleton = null;

    /**
     * 将构造器私有
     */
    private LazyDoubleCheckSingleton() {
    }

    /**
     * 双重检查
     * @return
     */
    public static LazyDoubleCheckSingleton getInstance() {
        if (lazyDoubleCheckSingleton == null) {
            synchronized (LazyDoubleCheckSingleton.class) {
                if (lazyDoubleCheckSingleton == null) {
                    lazyDoubleCheckSingleton = new LazyDoubleCheckSingleton();
                }
            }
        }
        return lazyDoubleCheckSingleton;
    }
}
